package com.qianfeng.wuyang.himalaya.fragment;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MyApplication;
import com.qianfeng.wuyang.himalaya.adapter.anchoradapter.Fragment_AnchorAdapt;
import com.qianfeng.wuyang.himalaya.bean.anchorbean.AnchorEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class AnchorFragment extends Fragment implements Callback<AnchorEntity>, SwipeRefreshLayout.OnRefreshListener {


    private RecyclerView recyclerView;
    //适配器
     private Fragment_AnchorAdapt adapt;
    private int pageNum=1;

    private   AnchorEntity body;

    private ProgressDialog progressDialog;
    private SwipeRefreshLayout swipeRefreshLayout;

    public AnchorFragment() {


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_anchor, container, false);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
          recyclerView = (RecyclerView) view.findViewById(R.id.anchor_recycle);
          swipeRefreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.fragment_anchor_swipefreshLayout);

        progressDialog = ProgressDialog.show(getContext(), "请稍等...","获取数据中...", true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                DownLoad(pageNum);
                progressDialog.dismiss();
            }
        }).start();

             adapt = new Fragment_AnchorAdapt(getContext());
             recyclerView.setAdapter(adapt);
             swipeRefreshLayout.setOnRefreshListener(this);
            recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                boolean isSwipeRefere=false;
                if(recyclerView.getChildCount()>0){
                    boolean numberOne=((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition()==0;
                    boolean isTop=recyclerView.getChildAt(0).getTop()==0;
                    if(numberOne && isTop){
                        isSwipeRefere=true;
                    }
                }
                swipeRefreshLayout.setEnabled(isSwipeRefere);
                //得到当前显示的最后一个item的view
                View lastChildView = recyclerView.getLayoutManager().getChildAt(recyclerView.getLayoutManager().getChildCount()-1);
                //得到lastChildView的bottom坐标值

                int lastChildBottom = lastChildView.getBottom();
                //得到Recyclerview的底部坐标减去底部padding值，也就是显示内容最底部的坐标
                int recyclerBottom =  recyclerView.getBottom()-recyclerView.getPaddingBottom();
                //通过这个lastChildView得到这个view当前的position值
                int lastPosition  = recyclerView.getLayoutManager().getPosition(lastChildView);

                if(lastChildBottom == recyclerBottom && lastPosition == recyclerView.getLayoutManager().getItemCount()-1 ){

                    pageNum=pageNum+1;
                    progressDialog = ProgressDialog.show(getContext(), "请稍等...","获取数据中...", true);
                     new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            DownLoad(pageNum);
                            progressDialog.dismiss();
                        }
                    }).start();
                    if(pageNum>3){
                        Toast.makeText(getContext(), "没有更多内容了！", Toast.LENGTH_SHORT).show();
                    }
                }

             }

        });


    }

    private void DownLoad(int pageNum) {
        MyApplication application = (MyApplication) getActivity().getApplication();
        application.getAanchorHttpUrl().getList(pageNum).enqueue(this);
    }


    @Override
    public void onResponse(Call<AnchorEntity> call, Response<AnchorEntity> response) {
        body = response.body();

        if (body.getList().size()>0) {
            Log.d("1620000", "onResponse: "+body.getList().size());
            adapt.addAll(body.getList());
            swipeRefreshLayout.setRefreshing(false);
        }
    }
    @Override
    public void onFailure(Call<AnchorEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();

    }


    @Override
    public void onRefresh() {
        adapt = new Fragment_AnchorAdapt(getContext());
        recyclerView.setAdapter(adapt);
         pageNum=1;
        DownLoad(pageNum);

    }
}
